package common.match
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.match.MatchDataManager;
	import com.liangzhong.model.type.MatchType;
	import com.lz.football.module.base.IComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import lz.controls.LItem;

	/**
	 * 
	 * @author lake
	 * 
	 */	
	public class MatchSmallItem extends LItem implements IComponent
	{
		/**
		 *标题 
		 */		
		private var txtTitle 	:TextField;
		/**
		 *分数 
		 */		
		private var txtScore	:TextField;
		/**
		 *时间 
		 */		
		private var txtTime	:TextField;
		/**
		 *关联的视图 
		 */		
		private var dispView	:DisplayObject;
		
		public function MatchSmallItem()
		{
			super();
		}
		/**
		 *获取设置数据 
		 * @return 
		 * 
		 */		
		override public function get data():*
		{
			return super.data;
		}
		override public function set data(value:*):void
		{
			super.data = value;
			dispView = _data.view ;
			if(dispView && dispView.parent)
			{
				dispView.visible = false;
				dispView.parent.removeChild(dispView);
			}
			var type :int = _data.type;
			switch(type)
			{
				//联赛
				case MatchType.PRE_MATCH:
				case MatchType.BASE_MATCH:
				case MatchType.CHAMPION_MATCH:
				case MatchType.LEGEND_MATCH:
				{
					_symbolSkin.gotoAndStop(1);
					break;
				}
				case MatchType.LADDYMATCH://天梯赛
				{
					_symbolSkin.gotoAndStop(2);
					break;
				}
				case MatchType.COUNTRY_MATCH://国家挑战赛
				{
					_symbolSkin.gotoAndStop(3);
					break;
				}
				case MatchType.FRIENDMATCH://友谊赛
				{
					_symbolSkin.gotoAndStop(4);
					break;
				}
			}
			//var selfName :String = AppData.getInstance().clubDatamanager.clubInfo.uname;
			//txtTitle.text = selfName +  "vs" + _data.name;
		}
		/**
		 *设置比分 
		 * @param selfScore
		 * @param opeScore
		 * 
		 */		
		public function setScore(selfScore:int,opeScore:int):void
		{
			//txtScore.text = selfScore + ":" + opeScore;
			_symbolSkin.mc.txtScore.text = selfScore + ":" + opeScore;
		}
		/**
		 *设置时间 
		 * @param value
		 * 
		 */		
		public function setTime(value :String):void
		{
			//txtTime.text = value + "";
			_symbolSkin.mc.txtTime.text = value + "";
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			this.buttonMode = true;
			this.mouseChildren = false;
			/*this.graphics.beginFill(0xcccccc,.8);
			this.graphics.drawRoundRect(0,0,150,40,10,10);
			this.graphics.endFill();
			
			var tlfFormat :TextFormat = new TextFormat();
			tlfFormat.align = TextFormatAlign.CENTER;
			
			txtTitle = new TextField();
			txtTitle.width = 100;
			txtTitle.height = 20;
			txtTitle.defaultTextFormat = tlfFormat;
			this.addChild(txtTitle);
			txtTitle.selectable = false;
			txtTitle.x = 20;
			txtTitle.y = 0;
			txtTitle.text = "比赛";
			txtTitle.mouseEnabled = false;
			
			tlfFormat = new TextFormat();
			tlfFormat.align = TextFormatAlign.LEFT;
			txtScore = new TextField();
			txtScore.width = 50;
			txtScore.height = 20;
			txtScore.defaultTextFormat = tlfFormat;
			this.addChild(txtScore);
			txtScore.selectable = false;
			txtScore.x = 80;
			txtScore.y = 20;
			txtScore.text = "0:0";
			txtScore.mouseEnabled = false;
			
			tlfFormat = new TextFormat();
			tlfFormat.align = TextFormatAlign.RIGHT;
			txtTime = new TextField();
			txtTime.width = 50;
			txtTime.height = 20;
			txtTime.defaultTextFormat = tlfFormat;
			this.addChild(txtTime);
			txtTime.selectable = false;
			txtTime.x = 20;
			txtTime.y = 20;
			txtTime.text = "00:00";
			txtTime.mouseEnabled = false;*/
			
			_symbolSkin = Reflection.createInstance("com.lz.football.match.SmallItemSKin");
			if(_symbolSkin)
			{
				this.addChild(_symbolSkin);
				_symbolSkin.mc.txtTime.text = "00:00";
			}
			addEvtListener();
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			this.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 * 条目点击
		 * @param e
		 * 
		 */		
		private function clickHandler(e :MouseEvent):void
		{
			if(dispView)
			{
				dispView.visible = true;
				GlobalDef.AlertContainer.addChild(dispView);
			}
			var index :int = MatchDataManager.SmallItemS.indexOf(this);
			if(index != -1)
			{
				MatchDataManager.SmallItemS.splice(index,1);
				MatchDataManager.SmallCount -- ;
				var i :int = 0;
				for each(var matchSmallItem:DisplayObject in MatchDataManager.SmallItemS)
				{
					GlobalDef.GameStage.addChild(matchSmallItem);
					matchSmallItem.x = (GlobalDef.GameStage.stage.stageWidth - matchSmallItem.width) - 10;
					matchSmallItem.y = (GlobalDef.GameStage.stage.stageHeight - matchSmallItem.height) - i * (matchSmallItem.height + 10) - 130;
					i ++;
				}
			}
			destroyView();
		}
		/**
		 *删除事件 
		 * 
		 */
		public function removeEvtListener():void
		{
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			dispView = null;
		}
	}
}